﻿package adpter.layer
{
	import adapter.common.DynamicObjectAdapter;
	import adapter.role.MainRoleAdapter;
	import adapter.role.RoleAdapterBase;
	
	import adpter.GameUIAdapter;
	
	import datapool.define.DirectionDefine;
	import datapool.define.IDZoneDefine;
	import datapool.global.GlobalInfo;
	
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	
	import ui.layer.RoleLayerUI;
	import ui.role.RoleBaseUI;
	
	public class RoleLayerAdapter extends DynamicObjectAdapter
	{
		private var _roles:Dictionary;
		
		public function RoleLayerAdapter()
		{
			super();
			init();
		}
		
		private function init():void
		{
			_roles = new Dictionary;
		}
		
		protected function get roleLayerUI():RoleLayerUI
		{
			return ui as RoleLayerUI;
		}
		
		public function update():void
		{
			var pos:Point;
			var roleAdapter:MainRoleAdapter = new MainRoleAdapter(0);
			var roleBaseUI:RoleBaseUI       = new RoleBaseUI();
			roleBaseUI.attachAdapter(roleAdapter);
			addRole(roleAdapter);
			
			
			pos = new Point(400,400);
			var roleAdapterBase:RoleAdapterBase = new RoleAdapterBase(1);
			roleAdapterBase.postion = pos;
			var roleBaseUI:RoleBaseUI = new RoleBaseUI();
			roleBaseUI.attachAdapter(roleAdapterBase);
			addRole(roleAdapterBase);
		}
		
		public function addRole(roleAdapter:RoleAdapterBase):void
		{
			if(roleAdapter)
			{
				_roles[roleAdapter.playerId] = roleAdapter;
				if(roleLayerUI)
				{
					roleLayerUI.addRole(roleAdapter.playerId);
				}
				
			}
		}
		
		public function removeRole(playerId:uint):void
		{
			if(_roles[playerId])
			{
				delete _roles[playerId];
				_roles[playerId] = null;
			}
		}
		
		public function get roles():Dictionary
		{
			return _roles;
		}
		
		override public function onFrame():void
		{
			super.onFrame();
			var roleAdapter:RoleAdapterBase;
			for each(roleAdapter in _roles)
			{
				roleAdapter.onFrame();
			}
		}
		
		public function gotoPos(pos:Point):void
		{
			var roleAdapter:RoleAdapterBase = _roles[0] as RoleAdapterBase;
			if(pos && roleAdapter)
			{
				roleAdapter.targetPostion = pos;
			}
		}
		
		public function updateViewRect():void
		{
			var roleAdapterBase:RoleAdapterBase;
			var pos:Point;
			if(roleLayerUI)
			{
				for each(roleAdapterBase in _roles)
				{
					updateRolePos(roleAdapterBase);
				}
			}
		}
		
		private function updateRolePos(role:RoleAdapterBase):void
		{
			var posOffset:Point;
			var newPos:Point;
			var speedPoint:Point;
			var x:int, y:int, width:int, hight:int;
			
			posOffset = new Point();
			newPos    = new Point();
			
			if(role)
			{
				x     = GlobalInfo.oppositePosX+role.postion.x;
				y     = GlobalInfo.oppositePosY+role.postion.y;
				width = GlobalInfo.stageWidth;
				hight = GlobalInfo.stageHight;

				if(role.isMainRole)
				{					
					speedPoint = DirectionDefine.caculateSpeed(new Point(role.absPostionX, role.absPostionY), role.targetPostion, role.speed);
					if(speedPoint)
					{
						if(Math.abs(role.postion.x+GlobalInfo.oppositePosX-role.targetPostion.x) <= role.speed)
						{

							posOffset.x = role.targetPostion.x - role.postion.x - GlobalInfo.oppositePosX;
						}
						else
						{
							posOffset.x = speedPoint.x;
						}
						
						if(Math.abs(role.postion.y+GlobalInfo.oppositePosY-role.targetPostion.y) <= role.speed)
						{
							posOffset.y = role.targetPostion.y - role.postion.y - GlobalInfo.oppositePosY;
						}
						else
						{
							posOffset.y = speedPoint.y;
						}
						
						if(x <= width/2 ||
							x >= MapLayerAdapter.mapWidth-width/2)
						{
							newPos.x = role.postion.x + posOffset.x;
						}
						else
						{
							newPos.x = role.postion.x;
							GlobalInfo.oppositePosX = GlobalInfo.oppositePosX+posOffset.x;
						}
						
						if(y <= hight/2 ||
							y >= MapLayerAdapter.mapHight-hight/2)
						{
							newPos.y = role.postion.y + posOffset.y;
						}
						else
						{
							newPos.y = role.postion.y;
							GlobalInfo.oppositePosY = GlobalInfo.oppositePosY+posOffset.y;
						}
						
						role.postion = newPos;
					}
				}
				else
				{
					newPos.x = role.postion.x;
					newPos.y = role.postion.y;
					newPos.x -= GlobalInfo.oppositePosX;
					newPos.y -= GlobalInfo.oppositePosY;
					role.updatePos(newPos);
				}
			}
		}
	}
}